package com.wing.ext.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.cms.domain.Artgroup;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.ext.dao.ProductgroupDao;
import com.wing.ext.domain.Productgroup;
import com.wing.sys.domain.Code;
/**
 * 
 * @author please write yout name
 * 
 */
@Service
@Transactional
public class ProductgroupManager {
	@Autowired
	private ProductgroupDao productgroupdao;
	
	
	/**
	 * 获得该对象所有数据的列表
	 * 
	 * @return list
	 */
	public List<Map<String,Object>> findAll(){
		return productgroupdao.findAll();	 
	}
	
	public PageBean findPageList(int currentPage, int pageSize){
		return productgroupdao.findPageList(currentPage,pageSize);	 
	}
	
	/**
	 * 通过id得到对象
	 *
	 * @param  id
	 * @return pObject
	 */
	public Productgroup findById(String id){
		return productgroupdao.findById(id);	 
	}
	
	/**
	 * 保存对象
	 *
	 * @param  productgroup
	 */
	public String saveProductgroup(Productgroup productgroup){
		if(productgroup.getId() == null ||  "".equals(productgroup.getId())){
			String id = new GUID().toString();
			productgroup.setId(id);
			productgroupdao.insert(productgroup);
			return id;
		}else{
			productgroupdao.update(productgroup);
			return productgroup.getId();
		}
	}

	/**
	 * 编辑对象
	 *
	 * @param  productgroup
	 */
	public void updateProductgroup(Productgroup productgroup){
		productgroupdao.update(productgroup);
	}
	
	 /**
	 * 删除对象
	 *
	 * @param  
	 */
	public void deleteProductgroups(String id) {
		productgroupdao.delete(id);
	}
	

	public List<Map<String, Object>> findChildByID(String parentid,
			String siteId) {
		return productgroupdao.findChildByID(parentid,siteId);
	}
	/********************************获取产品分类的xml开始*******************************
	 * @param siteId 
	 * @param  */
	
	public String createDHTMLXML(String parentid,boolean firstFlag, String siteId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		if (parentid.equalsIgnoreCase(Artgroup.ARTGROUPROOT) || firstFlag) {
			Productgroup rootGrp = productgroupdao.findById(parentid);
			buffer.append("<tree id=\"0\">");
			buffer.append("<item text=\""+rootGrp.getName()+"\" id=\""+rootGrp.getId()+"\" open=\"1\" select=\"1\">");
		} else {
			buffer.append("<tree id=\"" + parentid + "\">");
		}
		// 循环读取所有节点 
		List<Map<String, Object>> list = productgroupdao.findChildByID(parentid,siteId);
		for (Map map : list) {
			String id = (String)map.get("id");
			String name = (String)map.get("name");
			buffer.append("<item text= \"" + name + "\"");
			buffer.append(" id= \"" + id + "\"");

			if (grpIsLeaf(id,siteId)) {
				buffer.append(" im0=\"iconText.gif\" im1=\"iconTexts.gif\" im2=\"iconText.gif\"");
				buffer.append(" child=\"0\" >");
			} else {
				buffer.append(" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\"");
				buffer.append(" child=\"" + id + "\">");
			}
			buffer.append("</item>");
		}
		if (parentid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			buffer.append("</item>");
		}
		buffer.append("</tree>");
		return buffer.toString();
	}
	/**
	 * 判断该编码是否是叶子节点
	 * @param codeanization Id
	 * @return 
	 */
	private boolean grpIsLeaf(String id,String siteId){
		List list = productgroupdao.findChildByID(id,siteId);
		return list.size()<=0;
	}
	/********************************获取产品分类的xml结束********************************/

}



